home *** CD-ROM | disk | FTP | other *** search
- /* XGStdEditText.cpp
- *
- * This handles the standard editing control.
- */
-
- /* YAAF - Yet another application framework
- * Copyright (C) 1997 William Edward Woody and In Phase Consulting
- *
- * This library is free software; you can redistribute it
- * and/or modify it under the terms of the GNU Library
- * General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or any
- * later version.
- *
- * This library is distributed in the hope that it will be
- * useful, but WITHOUT ANY WARRANTY; without even the implied
- * warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Library General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Library General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- *
- * To contact the author, either e-mail me at
- * woody@alumni.caltech.edu, or write to us at
- *
- * William Edward Woody
- * In Phase Consulting
- * 1545 Ard Eevin Avenue
- * Glendale, CA 91202
- */
-
- #include <string.h>
- #include <XError.h>
- #include <XApplication.h>
- #include <XDataUtil.h>
- #include <XStdText.h>
- #include <XStdControls.h>
-
- /************************************************************************/
- /* */
- /* Construction/Destruction */
- /* */
- /************************************************************************/
-
- /* XGStdEditText::XGStdEditText
- *
- * Create this edit text box with the specified location
- */
-
- XGStdEditText::XGStdEditText(XGView *p, XGArgStream &s) :
- XGStdText(p,s,true)
- {
- char buffer[256];
- s.GetString(sizeof(buffer),buffer);
- Init(buffer);
- }
-
- XGStdEditText::XGStdEditText(XGView *p, XGSTextInitRecord &s) :
- XGStdText(p,s.v,true)
- {
- Init(s.text);
- }
-
- /* XGStdEditText::~XGStdEditText
- *
- * No longer edit the text in here
- */
-
- XGStdEditText::~XGStdEditText()
- {
- #if OPT_MACOS == 1
- if (fEdit) TEDispose(fEdit);
- #endif
- }
-
- /************************************************************************/
- /* */
- /* Standard Text Events */
- /* */
- /************************************************************************/
-
- /* XGStdEditText:DoDrawView
- *
- * Draw this thing
- */
-
- void XGStdEditText::DoDrawView(Rect r)
- {
- #if OPT_MACOS == 1
- Rect s;
-
- XGDraw draw(this,false);
-
- s = GetContentRect();
- ViewToGlobal(&s);
- DrawTextFrame(s);
-
- TEUpdate(&r,fEdit);
- #endif
-
- #if OPT_WINOS == 1
- #pragma unused(r)
- #endif
- }
-
- /* XGStdEditText::DoActivate
- *
- * Handle activation/deactivation (if this thing has the focus)
- */
-
- void XGStdEditText::DoActivate(bool active)
- {
- #if OPT_MACOS == 1
- XGDraw draw(this,false);
- if (HasFocus() && IsEnabled()) {
- if (active) TEActivate(fEdit);
- else TEDeactivate(fEdit);
- }
- #endif
-
- #if OPT_WINOS == 1
- #pragma unused(active)
- #endif
- }
-
- /* XGStdEditText::DoMouseDown
- *
- * Handle clicks in this thing
- */
-
- bool XGStdEditText::DoMouseDown(Point where, short flags)
- {
- #if OPT_MACOS == 1
- if (!IsEnabled()) return false;
- if (!HasFocus()) SetFocus(); /* I get the focus! */
-
- /*
- * Now handle the click
- */
-
- XGDraw draw(this,false);
- ViewToGlobal(&where);
-
- TEClick(where,(flags & KKeyShift) ? true : false,fEdit);
- return false;
- #endif
-
- #if OPT_WINOS == 1
- #pragma unused(where,flags)
- #endif
- }
-
- /* XGStdEditText::DoSizeView
- *
- * Resize this view
- */
-
- void XGStdEditText::DoSizeView()
- {
- #if OPT_MACOS == 1
- Rect r;
-
- XGDraw draw(this,false);
-
- r = GetContentRect();
- ViewToGlobal(&r);
- InsetRect(&r,3,3);
- (**fEdit).viewRect = r;
- if (r.right < 3000) r.right = 3000;
- (**fEdit).destRect = r;
- TECalText(fEdit);
- InvalView();
- #endif
- }
-
- /* XGStdEditText::DoMoveView
- *
- * Moves this view. Like DoSizeView
- */
-
- void XGStdEditText::DoMoveView()
- {
- #if OPT_MACOS == 1
- DoSizeView();
- #endif
- }
-
-
- /* XGStdEditText:GainFocus
- *
- * Gain the focus. This enables this thing
- */
-
- void XGStdEditText::GainFocus()
- {
- #if OPT_MACOS == 1
- if (IsActive() && IsEnabled()) {
- XGDraw draw(this,false);
- TEActivate(fEdit);
- }
- #endif
- }
-
- /* XGStdEditText::LoseFocus
- *
- * Lose the focus. This disables this thing
- */
-
- void XGStdEditText::LoseFocus()
- {
- #if OPT_MACOS == 1
- if (IsActive() && IsEnabled()) {
- XGDraw draw(this,false);
- TEDeactivate(fEdit);
- }
- #endif
- }
-
-
- /************************************************************************/
- /* */
- /* Dispatched events */
- /* */
- /************************************************************************/
-
- /* XGStdEditText::ReceiveDispatch
- *
- * This handles the following
- *
- * KEventIdle
- * KEventDoMenuCommand
- * Cut/Copy/Paste/Clear
- * KEventGetMenuStatus
- * Cut/Copy/Paste/Clear
- * KEventKey
- */
-
- long XGStdEditText::ReceiveDispatch(long msg, long arg, void *parg)
- {
- #if OPT_MACOS == 1
-
- XGSMenuStatusRecord *e;
- short cc;
-
- switch (msg) {
- case KEventIdle:
- if (IsActive() && IsEnabled() && HasFocus()) {
- XGDraw draw(this,false);
- TEIdle(fEdit);
- return 1;
- }
- break;
-
- case KEventGetMenuStatus:
- switch (arg) {
- case KMenuEditCut:
- case KMenuEditCopy:
- case KMenuEditPaste:
- case KMenuEditClear:
- e = (XGSMenuStatusRecord *)parg;
- e->enable = true;
- return 1;
- }
- break;
-
- case KEventDoMenuCommand:
- switch (arg) {
- case KMenuEditCut:
- {
- XGDraw draw(this,false);
- TECut(fEdit);
- return 1;
- }
- case KMenuEditCopy:
- {
- XGDraw draw(this,false);
- TECopy(fEdit);
- return 1;
- }
- case KMenuEditPaste:
- {
- XGDraw draw(this,false);
- TEPaste(fEdit);
- return 1;
- }
- case KMenuEditClear:
- {
- XGDraw draw(this,false);
- TEDelete(fEdit);
- return 1;
- }
- }
- break;
-
- case KEventKey:
- cc = GETHIWORD(arg);
- if (cc & KSpecialKey) {
- /* Trap and reconvert arrow keys back to Mac arrow keys */
- cc = GETLOWORD(arg);
- if (cc == KKeyLeftCursor) cc = 0x1C;
- else if (cc == KKeyRightCursor) cc = 0x1D;
- else if (cc == KKeyUpCursor) cc = 0x1E;
- else if (cc == KKeyDownCursor) cc = 0x1F;
- else break;
- } else {
- cc = GETLOWORD(arg);
- if (IsSpecialKey(cc)) break;
- }
-
- /*
- * My accepted key
- */
-
- {
- XGDraw draw(this,false);
- TEKey(cc,fEdit);
- return 1;
- }
-
- default:
- break;
- }
- #endif
-
- return XGView::ReceiveDispatch(msg,arg,parg);
- }
-
- /************************************************************************/
- /* */
- /* Miscellaneous Support */
- /* */
- /************************************************************************/
-
- /* XGView::SetSelect
- *
- * Set the current selection
- */
-
- void XGStdEditText::SetSelect(short s, short e)
- {
- #if OPT_MACOS == 1
- XGDraw draw(this,false);
- TESetSelect(s,e,fEdit);
- #endif
-
- #if OPT_WINOS == 1
- SendMessage(_GetHWND(),EM_SETSEL,(WPARAM)s,(LPARAM)e);
- #endif
- }
-
- /* XGView::GetSelect
- *
- * Get the current selection range
- */
-
- void XGStdEditText::GetSelect(short *s, short *e)
- {
- #if OPT_MACOS == 1
- *s = (**fEdit).selStart;
- *e = (**fEdit).selEnd;
- #endif
-
- #if OPT_WINOS == 1
- long ss,ee;
-
- SendMessage(_GetHWND(),EM_GETSEL,(WPARAM)(LPDWORD)&ss,(LPARAM)(LPDWORD)&ee);
- *s = ss;
- *e = ee;
- #endif
- }
-
- /************************************************************************/
- /* */
- /* Text manipulation */
- /* */
- /************************************************************************/
-
- /* XGStdEditText::SetText
- *
- * Set the text in this thing
- */
-
- void XGStdEditText::SetText(char *text)
- {
- #if OPT_MACOS == 1
- XGDraw draw(this,false);
-
- TESetText(text,strlen(text),fEdit);
- TESetSelect(0,strlen(text),fEdit);
- #endif
-
- #if OPT_WINOS == 1
- ::SetWindowText(_GetHWND(),text);
- #endif
- }
-
- /* XGStdEditText::GetText
- *
- * Get the text from this thing
- */
-
- void XGStdEditText::GetText(char *text)
- {
- #if OPT_MACOS == 1
- short len = 255;
- if (len > (**fEdit).teLength) len = (**fEdit).teLength;
- BlockMove(*((**fEdit).hText),text,len);
- text[len] = 0;
- #endif
-
- #if OPT_WINOS == 1
- ::GetWindowText(_GetHWND(),text,256);
- #endif
- }
-
- /************************************************************************/
- /* */
- /* Miscellaneous Hooks */
- /* */
- /************************************************************************/
-
- /* XGView::IsSpecialKey
- *
- * This determines the 'special keys'--those keys which are passed
- * up to the containing view. By default, these are the enter key and
- * the tab key
- */
-
- bool XGStdEditText::IsSpecialKey(char c)
- {
- if (c == 0x0D) return true;
- if (c == 0x09) return true;
- return false;
- }
-
- /* XGView::DrawTextFrame
- *
- * This draws the frame around the edited text
- */
-
- void XGStdEditText::DrawTextFrame(Rect r)
- {
- #if OPT_MACOS == 1
- FrameRect(&r);
- InsetRect(&r,1,1);
- EraseRect(&r);
- #endif
-
- #if OPT_WINOS == 1
- #pragma unused(r)
- #endif
- }
-
-
- /************************************************************************/
- /* */
- /* Initialization */
- /* */
- /************************************************************************/
-
- /* XGStdEditText::Init
- *
- * Initialize this thing
- */
-
- void XGStdEditText::Init(char *buffer)
- {
- long l;
-
- l = GetParent()->ReceiveDispatch(KEventGetFont,GetViewID(),(void *)&this);
- #if OPT_MACOS == 1
- Rect r;
- Rect t;
- XGDraw draw(this,false);
-
- ::TextFont(GETHIWORD(l));
- ::TextSize(GETLOWORD(l));
-
- /*
- * Create a textedit handle
- */
-
- r = GetContentRect();
- ViewToGlobal(&r);
- InsetRect(&r,3,3); /* Inset away from border */
- t = r;
- if (t.right < 3000) t.right = 3000; /* Arbitrary big number */
-
- fEdit = TENew(&t,&r); /* Dest rect wider... */
-
- /*
- * Initialize this thing
- */
-
- TESetText(buffer,strlen(buffer),fEdit);
- TEAutoView(true,fEdit);
- TESetSelect(0,strlen(buffer),fEdit);
- #endif
-
- #if OPT_WINOS == 1
- Rect r;
- HWND w;
-
- /*
- * Get the location of the control in the parent's coordinate
- * system
- */
-
- r = GetContentRect();
- if (GetParent()) {
- ViewToGlobal(&r);
- GetParent()->GlobalToView(&r);
- }
-
- /*
- * Create the control window
- */
-
- w = CreateWindow("EDIT", // control class
- buffer, // control title
- WS_CHILD | ES_AUTOHSCROLL | // control window flags
- WS_BORDER,
- r.left,
- r.top,
- r.right-r.left,
- r.bottom-r.top, // control location
- GetParent()->_GetHWND(), // container window
- NULL, // no menu
- _GInstance, // My app instance
- NULL); // no additional args
- if (w == NULL) {
- throw XPostError("Unable to make editbox");
- }
-
- _SetHWND(w); // set me as the window
- SetProp(w,_GViewClass,(HANDLE)this);
- if (IsVisible()) ShowWindow(w,SW_SHOW);
- EnableWindow(w,IsEnabled()); // set me up
- #endif
- }
-